#ifndef MYMODBUS_H
#define MYMODBUS_H
#include <QDebug>

#define MODBUS_SERVER_IP "127.0.0.1"
#define MODBUS_SERVER_PORT 9999

extern "C"{
#include <stdio.h>
#include <string.h>
#include <modbus.h>
#include <modbus/modbus-tcp.h>
#include <unistd.h>

    static modbus_t *modbus_connect_to_server(modbus_t *ctx, uint16_t tab_reg[], int rc);
    static int collect_data_from_server(modbus_t *ctx, uint16_t tab_reg[], int rc);
    static int disconnect_from_server(modbus_t *ctx);
}



class Modbus
{
public:


    Modbus();

public:
    static modbus_t *modbus_connect_to_server(modbus_t *ctx, uint16_t tab_reg[], int rc){
        // Create a new modbus TCP context
        ctx = modbus_new_tcp(MODBUS_SERVER_IP, MODBUS_SERVER_PORT);
        if (ctx == NULL) {
            fprintf(stderr, "Unable to create the Modbus TCP context\n");
            return NULL;
        }

        // Connect to the Modbus server
        if (modbus_connect(ctx) == -1) {
            fprintf(stderr, "Modbus connection failed: %s\n", modbus_strerror(errno));
            modbus_free(ctx);
            return NULL;
        }


        rc = modbus_read_registers(ctx, 0, 4, tab_reg);
        if (rc == -1) {
            fprintf(stderr, "Failed to read Modbus registers: %s\n", modbus_strerror(errno));
            modbus_close(ctx);
            modbus_free(ctx);
            return NULL;
        }

        // Encrypt data before transmission
        tab_reg[0] = tab_reg[0] + 10;
        tab_reg[1] = tab_reg[1] + 5;
        tab_reg[2] = tab_reg[2] + 10;
        tab_reg[3] = tab_reg[3] + 5;

        printf("Temperature: %f C, Humidity: %f%%,Temperature2: %f C, Humidity2: %f\n", tab_reg[0], tab_reg[1], tab_reg[2], tab_reg[3]);
        qDebug() << "Temperature:" << tab_reg[0] << "°c";
        qDebug() << "Humidity:" << tab_reg[1] << "%";
        qDebug() << "Temperature2:" << tab_reg[2] << "°c";
        qDebug() << "Humidity2:" << tab_reg[3] << "%";

        return ctx;
    }

    static int collect_data_from_server(modbus_t *ctx, uint16_t tab_reg[], int rc){
        // Read temperature and humidity values from Modbus registers
        rc = modbus_read_registers(ctx, 0, 4, tab_reg);
        if (rc == -1) {
            fprintf(stderr, "Failed to read Modbus registers: %s\n", modbus_strerror(errno));
            modbus_close(ctx);
            modbus_free(ctx);
            return -1;
        }

        // Encrypt data before transmission
        tab_reg[0] = tab_reg[0] + 10;
        tab_reg[1] = tab_reg[1] + 5;
        tab_reg[2] = tab_reg[2] + 10;
        tab_reg[3] = tab_reg[3] + 5;

        printf("Temperature: %d C, Humidity: %d%%\n", tab_reg[0], tab_reg[1]);
        qDebug() << "Temperature:" << tab_reg[0] << "°c";
        qDebug() << "Humidity:" << tab_reg[1] << "%";
        qDebug() << "Temperature2:" << tab_reg[2] << "°c";
        qDebug() << "Humidity2:" << tab_reg[3] << "%";

        return 1;

    }

    static int disconnect_from_server(modbus_t *ctx){
        // Disconnect from the Modbus server
        modbus_close(ctx);
        modbus_free(ctx);

    }

private:




public:
    bool if_modbus_connect_on;

public:

};

#endif // MYMODBUS_H
